package com.ahy231.controller;

import com.ahy231.dto.*;
import com.ahy231.service.*;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2021/8/2 16:05
 * @description 车辆
 */
@Api(tags = "车辆")
@RestController
@RequestMapping("/truck")
public class TruckController {

    private final TruckService truckService;

    public TruckController(TruckService truckService) {
        this.truckService = truckService;
    }

    @ApiOperation("添加车辆")
    @PostMapping(value = "/add")
    @RequiresRoles(value = {"driver", "company"}, logical = Logical.OR)
    public Result addTruck(@ApiParam("车辆信息（id 不用填写）") @RequestBody TruckInfo truck) {
        try {
            return new Result(ResultCode.SUCCESS, truckService.addTruck(truck));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("获取所有车辆信息")
    @PostMapping("/get")
    @RequiresRoles(value = {"driver", "company"}, logical = Logical.OR)
    public Result getTrucks() {
        try {
            return new Result(ResultCode.SUCCESS, truckService.getTrucks());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("移除车辆信息")
    @DeleteMapping("/delete")
    @RequiresRoles(value = {"driver", "company"}, logical = Logical.OR)
    public Result removeTruck(@ApiParam("车辆 id") @RequestParam Long truckId) {
        try {
            return new Result(ResultCode.SUCCESS, truckService.removeTruck(truckId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("设置车辆闲置状态")
    @PostMapping("/status")
    @RequiresRoles(value = {"driver", "company"}, logical = Logical.OR)
    public Result setStatus(@ApiParam("车辆 id") @RequestParam Long truckId, @ApiParam("状态") @RequestParam Integer status) {
        try {
            return new Result(ResultCode.SUCCESS, truckService.setStatus(truckId, status));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
